#!/usr/bin/env python
#-*- coding:utf-8 -*-

'''
Created on 2016年5月16日

@author: guoff
'''
from Tkinter import *
from ttk import Notebook
from Tkinter import Frame
from Tkinter import Label
from words import *

'''
系统函数
'''
filename=''

def Command_openfile():
    import tkFileDialog
    global filename 
    filename = tkFileDialog.askopenfilenames(initialdir = os.curdir,filetypes=[("text file", "*.txt"),("all","*.*")])
    file_cup = filename#repr(filename)
    print(file_cup)
    print(len(file_cup))
    filename = filename
    Text1.insert(0, filename)
    file_str = ''
    count=0
    for  item  in  range(len(file_cup)):  
        tmp_str = u'%d、%s\n' % (item+1, file_cup[item]) 
        file_str += tmp_str
        count=count+1
    Text3.insert('1.0', file_str)
    Label2['text']=u'文件总数：%d' % (count) 

def Command_dofile():
    do_file(filename)

master = Tk()
master.title(u'语料分析系统')
master.geometry('671x413')

noteBook = Notebook(master,height='671',width='673')

file_Frame = Frame(noteBook)
sum_Frame = Frame(noteBook)
juzi_Frame = Frame(noteBook)
sgui_Frame = Frame(noteBook)



def show_file(event):
    index = noteBook.select()
    print(noteBook.tab(index)['text'])
    if noteBook.tab(index)['text']==u'分词统计':
        cu = get_cursor()
        sql = "select id,word,count,file_count from word order by count desc "
        cursor = cu.execute(sql)
        #self.Text3.insert('20',u'序号'.center(15)+u'词汇'.center(21)+u'频率'.center(16)+u'文件数'.center(15)+"\n")
        i=1
        for item in cursor:
            sum_Text3.insert('10000.0',str(i).center(15)+str(item[2]).center(16)+str(item[3]).center(15)+item[1].center(21)+"\n")
            i=i+1
    
    if noteBook.tab(index)['text']==u'语句识别':
        cu = get_cursor()
        sql = "select text,count(*) from sentence group by text"
        cursor = cu.execute(sql)
        #self.Text3.insert('2.0',u'序号'.center(15)+u'词汇'.center(21)+u'频率'.center(16)+u'文件数'.center(15)+"\n")
        i=1
        for item in cursor:
            juzi_Text3.insert('1000000000.0',str(i).center(5)+str(item[1]).center(6)+item[0].ljust(100)+"\n")
            i=i+1
    
def sgui_Command1_Cmd(event=None):
    word = sgui_Text1Var.get().strip()
    count = int(sgui_Text2Var.get().strip())

    result = generate(word)

    rs = ''
    cu = get_cursor()
    sql = "select sum(count),sum(file_count) from word where word like '%"+word+"%'"
    cursor = cu.execute(sql)
    #sgui_Text3.insert('2.0',u'序号'.center(15)+u'词汇'.center(21)+u'频率'.center(16)+u'文件数'.center(15)+"\n")
    for item in cursor:
        rs += u'词频:%s\n' % str(item[0])
        rs += u'分布:%s\n' % str(item[1])
    #select * from sentence where text like '%资源%'
    sql = "select text from sentence where text like '%"+word+"%'"
    cursor = cu.execute(sql)
    sentences = result['sentences']
    i=0
    for item in cursor:
        if count <= i:
            break
        rs += u'%d、%s\n' % (i+1, item[0])
        i=i+1

    sgui_Text3.delete(1.0, END)
    sgui_Text3.insert('1.0', rs)


noteBook.add(file_Frame, text=u'语料读入')
noteBook.add(sum_Frame, text=u'分词统计')
noteBook.add(juzi_Frame, text=u'语句识别')
noteBook.add(sgui_Frame, text=u'例句生成')
# Change window size each tab tkinter notebook

noteBook.pack(side=LEFT)
Label1 = Label(file_Frame, text=u'语料文件路径：', anchor='w')
Label1.place(x=32, y=32, width=103, height=25)
#Label1.pack(side=LEFT,expand=YES,padx=1,anchor=NW)
Text1Var = StringVar(value=u'')
Text1 = Entry(file_Frame, textvariable=Text1Var)
Text1.place(x=152, y=32, width=353, height=25)

B = Button(file_Frame, text =u"浏览", command = Command_openfile)
B.place(x=520, y=32,width=50)


Text3 = Text(file_Frame)
Text3.place(x=40, y=100, width=585, height=201)
Text3.insert('1.0','')

Label2 = Label(file_Frame, text=u'文件总数：0', anchor='w')
Label2.place(x=132, y=320, width=103, height=25)
B = Button(file_Frame, text =u"分析", command = Command_dofile)
B.place(x=320, y=320,width=50)


sum_Label1 = Label(sum_Frame, text=u'  序号', anchor='w')
sum_Label1.place(x=62, y=32, width=50, height=25)

sum_Label3 = Label(sum_Frame, text=u'  频率', anchor='w')
sum_Label3.place(x=162, y=32, width=50, height=25)

sum_Label4 = Label(sum_Frame, text=u'文件数', anchor='w')
sum_Label4.place(x=182+121, y=32, width=200, height=25)

sum_Label2 = Label(sum_Frame, text=u'               词汇', anchor='w')
sum_Label2.place(x=182+161+51, y=32, width=150, height=25)

sum_Text3 = Text(sum_Frame)
sum_Text3.place(x=40, y=60, width=585, height=301)
sum_Text3.insert('1.0','')


juzi_Label1 = Label(juzi_Frame, text=u'  序号', anchor='w')
juzi_Label1.place(x=32, y=32, width=50, height=25)

juzi_Label4 = Label(juzi_Frame, text=u'文件数', anchor='w')
juzi_Label4.place(x=82, y=32, width=250, height=25)

juzi_Label2 = Label(juzi_Frame, text=u'               句子', anchor='w')
juzi_Label2.place(x=232, y=32, width=350, height=25)

juzi_Text3 = Text(juzi_Frame)
juzi_Text3.place(x=40, y=60, width=585, height=301)
juzi_Text3.insert('1.0','')



sgui_Label1 = Label(sgui_Frame, text=u'词汇：', anchor='w')
sgui_Label1.place(x=32, y=32, width=73, height=25)

sgui_Text1Var = StringVar(value=u'自己')
sgui_Text1 = Entry(sgui_Frame, textvariable=sgui_Text1Var)
sgui_Text1.place(x=112, y=32, width=153, height=25)

sgui_Label2 = Label(sgui_Frame, text=u'例句个数：', anchor='w')
sgui_Label2.place(x=312, y=32, width=97, height=25)

sgui_Text2Var = StringVar(value='5')
sgui_Text2 = Entry(sgui_Frame, textvariable=sgui_Text2Var)
sgui_Text2.place(x=416, y=32, width=145, height=25)

sgui_Command1 = Button(sgui_Frame, text=u'生成', command=sgui_Command1_Cmd)
sgui_Command1.place(x=240, y=80, width=137, height=33)

sgui_Text3 = Text(sgui_Frame)
sgui_Text3.place(x=40, y=136, width=585, height=201)
sgui_Text3.insert('1.0','')

noteBook.bind("<<NotebookTabChanged>>", show_file)
master.mainloop()